package com.zc.im.seqsvr.converter;

import com.zc.im.common.constant.enums.BaseEnum;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Project：im
 * Date：2022/4/21
 * Time：10:11
 * Description：int转枚举转换器工厂
 *
 * @author Challen.Zhang
 * @version 1.0
 */
@ReadingConverter
public class IntegerToBaseEnumConverterFactory<T extends BaseEnum<Integer>> implements ConverterFactory<Integer, T> {

    private Map<Class<? extends BaseEnum<Integer>>, Converter<Integer, ? extends BaseEnum<Integer>>> map = new HashMap<>();

    @Override
    public <T1 extends T> Converter<Integer, T1> getConverter(Class<T1> targetType) {
        Converter<Integer, ? extends BaseEnum<Integer>> integerConverter = map.get(targetType);
        if (integerConverter == null) {
            integerConverter = new IntegerToBaseEnumConverter<>(targetType);
            map.put(targetType, integerConverter);
        }
        return (Converter<Integer, T1>) integerConverter;
    }
}
